package com.eleme.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleme.business.dto.BusinessRegisterDTO;
import com.eleme.business.dto.BusinessUpdateDTO;
import com.eleme.business.entity.Business;
import com.eleme.business.mapper.BusinessMapper;
import com.eleme.business.service.BusinessService;
import com.eleme.business.vo.BusinessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public String register(BusinessRegisterDTO dto) {
        QueryWrapper<Business> query = new QueryWrapper<>();
        query.eq("business_name", dto.getBusinessName());
        if (businessMapper.selectCount(query) > 0) {
            throw new RuntimeException("商家名称已存在");
        }
        Business business = new Business();
        BeanUtils.copyProperties(dto, business);
        business.setBusinessId(UUID.randomUUID().toString().replace("-", ""));
        business.setStatus(0);
        business.setCreateTime(new Date());
        businessMapper.insert(business);
        return "商家注册成功";
    }

    @Override
    public String update(BusinessUpdateDTO dto) {
        if (!StringUtils.hasText(dto.getBusinessId())) {
            throw new RuntimeException("商家ID不能为空");
        }
        Business business = businessMapper.selectById(dto.getBusinessId());
        if (business == null) {
            throw new RuntimeException("商家不存在");
        }
        BeanUtils.copyProperties(dto, business);
        business.setUpdateTime(new Date());
        businessMapper.updateById(business);
        return "商家信息更新成功";
    }

    @Override
    public String changeStatus(String businessId, Integer status) {
        Business business = businessMapper.selectById(businessId);
        if (business == null) throw new RuntimeException("商家不存在");
        business.setStatus(status);
        businessMapper.updateById(business);
        return status == 1 ? "商家已禁用" : "商家已启用";
    }

    @Override
    public List<BusinessVO> listAll() {
        List<Business> list = businessMapper.selectList(null);
        List<BusinessVO> res = new ArrayList<>();
        for (Business b : list) {
            BusinessVO vo = new BusinessVO();
            BeanUtils.copyProperties(b, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    public List<BusinessVO> listByType(String type) {
        QueryWrapper<Business> query = new QueryWrapper<>();
        query.eq("business_type", type);
        List<Business> list = businessMapper.selectList(query);
        List<BusinessVO> res = new ArrayList<>();
        for (Business b : list) {
            BusinessVO vo = new BusinessVO();
            BeanUtils.copyProperties(b, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    public List<BusinessVO> searchByName(String name) {
        QueryWrapper<Business> query = new QueryWrapper<>();
        query.like("business_name", name);
        List<Business> list = businessMapper.selectList(query);
        List<BusinessVO> res = new ArrayList<>();
        for (Business b : list) {
            BusinessVO vo = new BusinessVO();
            BeanUtils.copyProperties(b, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    public BusinessVO getById(String businessId) {
        Business b = businessMapper.selectById(businessId);
        if (b == null) return null;
        BusinessVO vo = new BusinessVO();
        BeanUtils.copyProperties(b, vo);
        return vo;
    }
} 